import random

from django.shortcuts import render
from django.http import HttpResponse
from myadmin.models import User
from django.core.paginator import Paginator
from datetime import datetime
import hashlib,random
from myadmin.models import Member

def index(request,pagIndex=1):
    umod = Member.objects
    ulist = umod.filter(status__lt=9)
    mywhere = []
    #获取判断并封装status的搜索条件
    status = request.GET.get('status','')
    if status != '':
        ulist = ulist.filter(status=status)
        mywhere.append('status='+status)

    #执行分页处理
    pagIndex = int(pagIndex)
    page = Paginator(ulist,10)
    maxpages = page.num_pages

    #判断当前分页是否越界
    if pagIndex > maxpages:
        pagIndex = maxpages
    if pagIndex < 1 :
        pagIndex = 1

    list2 = page.page(pagIndex) #获取当前页数据
    plist = page.page_range #读取页码列表信息

    context ={'memberlist':list2,'plist':plist,'pagIndex':pagIndex,'maxpages':maxpages,'mywhere':mywhere}
    return render(request,'myadmin/member/index.html',context)


def delete(request,uid=0):
    try:
        ob = User.objects.get(id=uid)
        ob.status = 9
        ob.update_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        ob.save()
        context = {'info':'删除成功！'}

    except Exception as err :
        print(err)
        context = {'info':'删除失败！'}
    return render(request,'myadmin/info.html',context)

